package com.hengda.museumonline.ui;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.webkit.WebView;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.google.gson.Gson;
import com.hengda.museumonline.MyApplication;
import com.hengda.museumonline.R;
import com.hengda.museumonline.entity.EncBean;
import com.hengda.museumonline.http.CallServer;
import com.litesuits.common.utils.PackageUtil;
import com.yolanda.nohttp.NoHttp;
import com.yolanda.nohttp.RequestMethod;
import com.yolanda.nohttp.rest.OnResponseListener;
import com.yolanda.nohttp.rest.Request;
import com.yolanda.nohttp.rest.Response;

import static com.yolanda.nohttp.NoHttp.getContext;

/**
 * @author：JFWU on 2016/10/19 20:24
 * @E-mail：jeffy12138@126.com
 */
public class EncContentActivity extends BaseActivity {
    private int did;
    private EncBean encBean;
    private android.widget.ImageView image;
    private android.widget.TextView title;
    private android.widget.TextView views;
    private android.widget.TextView edits;
    private android.widget.TextView lastedit;
    private android.widget.TextView author;
    private WebView content;
    private android.widget.TextView typename;
    static final String mimeType = "text/html";
    static final String encoding = "utf-8";

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_enccontent);
        this.typename = (TextView) findViewById(R.id.typename);
        this.content = (WebView) findViewById(R.id.content);
        content.setBackgroundColor(0);
        content.getBackground().setAlpha(0);
        this.author = (TextView) findViewById(R.id.author);
        this.lastedit = (TextView) findViewById(R.id.lastedit);
        this.edits = (TextView) findViewById(R.id.edits);
        this.views = (TextView) findViewById(R.id.views);
        this.title = (TextView) findViewById(R.id.title);
        this.image = (ImageView) findViewById(R.id.image);
        did = Integer.parseInt(getIntent().getStringExtra("did"));
        getContentfromNet();
        System.out.println("did:" + did);
    }

    private void getContentfromNet() {
        Request request = NoHttp.createStringRequest(MyApplication.url + "wiki/info", RequestMethod.GET);
        request.add("p", "a");
        request.add("v", PackageUtil.getAppPackageInfo(this).versionName);
        request.add("did", did);

        CallServer.getRequestInstance().add(this, 0, request, new OnResponseListener() {
            @Override
            public void onStart(int what) {
            }

            @Override
            public void onSucceed(int what, Response response) {
                System.out.println("response:" + response.get().toString());
                if (200 == response.getHeaders().getResponseCode()) {
                    Gson gson = new Gson();
                    encBean = gson.fromJson(response.get().toString(), EncBean.class);
                    if (encBean != null && encBean.getData() != null && encBean.getData().getList() != null) {

                        setData(encBean);
                    }
                }
            }

            @Override
            public void onFailed(int what, String url, Object tag, Exception exception, int responseCode, long networkMillis) {
                System.out.println("onFailed:" + exception);
            }

            @Override
            public void onFinish(int what) {
            }
        });
    }

    private void setData(EncBean encBean) {
        Glide.with(getContext()).load(encBean.getData().getList().getImage()).into(image);
        title.setText(encBean.getData().getList().getTitle());
        views.setText("浏览次数：" + encBean.getData().getList().getViews() + "次");
        edits.setText("编辑次数：" + encBean.getData().getList().getEdits() + "次");
        lastedit.setText("更新时间：" + encBean.getData().getList().getLastedit());
        author.setText("创建者：" +encBean.getData().getList().getAuthor());
        content.loadDataWithBaseURL(null, encBean.getData().getList().getContent(), mimeType, encoding, null);
        typename.setText("分类：" +encBean.getData().getList().getTypename());
    }
}
